/**
 * 
 */
package net.mikoo.seals.common.model;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import net.mikoo.seals.common.WebContext;

/**
 * @author huangxm
 * 
 */
public class PageModel<T> {

	private  Integer pageSize = 10;

	private Integer currentPage = 0;

	/** 总条数目 */
	private Integer total;

	/** 分页数据 */
	private List<T> datas;

	/** 总页数 */
	private Integer totalPage;

	/** 首页 */
	private Integer first = 1;

	/** 上一页 */
	private Integer previous = 1;

	/** 下一页 */
	private Integer next;

	/** 尾页 */
	private Integer last = 0;
	
	private String url;

	private List<Integer> pageList;
/**
 * 
 * @param total 总条数
 * @param datas 数据
 * @param currentPage 当前页
 */
	
	
	public PageModel(Integer total, List<T> datas) {
		
		if(total == null){
			total = 0;
		}
		
		this.total = total;
		this.datas = datas;
		this.currentPage = WebContext.getPage();
		this.totalPage = countTotalPage(total);
		this.pageList = this.getPageList(currentPage, totalPage, 5);
		this.url = transformURI();
	}
	
	public PageModel(Integer total, List<T> datas ,Integer pageSize) {
		
		this.total = total;
		this.datas = datas;
		this.currentPage = WebContext.getPage();
		this.pageSize = pageSize;
		this.totalPage = countTotalPage(total);
		this.pageList = this.getPageList(currentPage, totalPage, 5);
		this.url = transformURI();
	}

	public Integer getFirst() {
		return first;
	}

	public Integer getPrevious() {
		if(currentPage > totalPage){
			currentPage = totalPage;
		}

		if (this.currentPage <= 1) {
			return 1;
		}

		previous = currentPage - 1;
		if ((currentPage - 1) < 1) {
			return 1;
		}

		return previous;
	}

	public Integer getNext() {
		
		if(currentPage > totalPage){
			currentPage = totalPage;
		}
		
		next = currentPage + 1;
		if (next > totalPage) {
			next = totalPage;
		}
		return next;
	}

	public Integer getLast() {

		last = totalPage;
		return last;
	}

	private  synchronized Integer countTotalPage(Integer total) {

		Integer pageCount = (total + pageSize - 1) / pageSize;

		return pageCount;
	}

	/**
	 * 
	 * @param nowpage
	 *            当前页
	 * @param totalPage
	 *            总页数
	 * @param sumindex
	 *            显示分页数
	 * @return
	 */
	private List<Integer> getPageList(Integer currentPage, Integer totalPage,
			Integer sumindex) {
		if(currentPage == null) {
			return null;
		}
		
		if(currentPage > totalPage){
			currentPage = totalPage;
		}

		int startIndex = 0;
		int endIndex = 0;

		startIndex = currentPage - (sumindex / 2);
		endIndex = currentPage + (sumindex / 2);

		// 如果起始 小于 为1
		if (startIndex < 1) {

			startIndex = 1;
			endIndex = sumindex;
		}

		if (startIndex < 1 || totalPage < sumindex) {

			startIndex = 1;
			endIndex = totalPage;
		}

		if (endIndex > totalPage) {

			endIndex = totalPage;
			startIndex = (totalPage - sumindex) + 1;
		}

		// System.out.println(startIndex + " - " + endIndex);

		/*
		 * map.put("startindex", startIndex); map.put("endindex", endIndex+1);
		 */

		List<Integer> lsit = new ArrayList<Integer>();
		for (int i = startIndex; i < (endIndex + 1); i++) {
			lsit.add(i);
		}

		return lsit;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	public List<T> getDatas() {
		return datas;
	}

	public void setDatas(List<T> datas) {
		this.datas = datas;
	}

	public Integer getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}

	public Integer getCurrentPage() {
		
		if(currentPage > totalPage){
			currentPage = totalPage;
		}
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public List<Integer> getPageList() {
		return pageList;
	}

	public void setPageList(List<Integer> pageList) {
		this.pageList = pageList;
	}
	
	
	public String getUrl() {
		
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	

	public String transformURI(){
		String url =  WebContext.getUrl();
		if(StringUtils.isNotEmpty(url)){
			
			int index = url.indexOf("page=");
			if(index >= 0){
				url = url.substring(0, index-1);
			}
			
			if(url.indexOf("?")>=0){
				url += "&page=";
			}else{
				url += "?page=";
			}
		}
		return url;
	}
		

}
